package com.ymttest.database.sqlwapper.godmch;

import com.ymt.core.tool.FormatUtil;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.*;
import com.ymttest.database.model.godmch.chargeconfig;
import com.ymttest.database.model.godmch.chargeconfigExample;
import com.ymttest.database.model.godmch.subscriptioncharge;
import com.ymttest.database.model.godmch.subscriptionchargeExample;
import com.ymttest.database.model.godmch.subscriptionfunc;
import com.ymttest.database.model.godmch.subscriptionfuncExample;
import com.ymttest.database.model.godmch.subscriptionfuncExample.Criteria;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefGodMchWapper {
	SqlManager manage;

	public DefGodMchWapper() {
		manage = new SqlManager("mysqlgodmchdbsit1");

	}
	
	
	/**
	 * 用MchId查询订购服务表
	 * 
	 * @param MchId 
	 * @return
	 * @throws InterruptedException
	 */
	public List<subscriptionfunc> selectSubscriptionFuncByMchId(Integer MchId) throws InterruptedException {
		subscriptionfuncExample pe = new subscriptionfuncExample();
		pe.setOrderByClause("`UpdateTime` DESC");
		Criteria c = pe.createCriteria();
		c.andMchIdEqualTo(MchId);
		List<subscriptionfunc> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.subscriptionfuncMapper.selectByExample", pe);
		manage.commit();
		return list;
	}
	
	/**
	 * 用MchId查询订单缴费表
	 * 
	 * @param MchId 
	 * @return
	 * @throws InterruptedException
	 */
	public List<subscriptioncharge> selectSubscriptionChargeByMchId(Integer MchId) throws InterruptedException {
		subscriptionchargeExample pe = new subscriptionchargeExample();
		pe.setOrderByClause("`UpdateTime` DESC");
		com.ymttest.database.model.godmch.subscriptionchargeExample.Criteria c = pe.createCriteria();
		c.andMchIdEqualTo(MchId);
		List<subscriptioncharge> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.subscriptionchargeMapper.selectByExample", pe);
		manage.commit();
		return list;
	}
	
	/**
	 * 用将该用户的订购结束日期更改为前一天
	 * 
	 * @param MchId 
	 * @return
	 * @throws InterruptedException
	 */
	public int UpdateSubscriptionTime(Integer MchId) throws InterruptedException {
		subscriptionfuncExample pe = new subscriptionfuncExample();
		Criteria c = pe.createCriteria();
		c.andMchIdEqualTo(MchId);
		List<subscriptionfunc> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.subscriptionfuncMapper.selectByExample", pe);
		
		subscriptionfunc subscriptionfunc = new subscriptionfunc();
		subscriptionfunc.setSubscriptionFuncId(list.get(0).getSubscriptionFuncId());
		subscriptionfunc.setSubscriptionExpirationTime(YMTDateUtil.getBeforeOrNextDate(-1));
		int update = manage.update("com.ymttest.database.mapping.godmch.subscriptionfuncMapper.updateByPrimaryKeySelective", subscriptionfunc);
		manage.commit();
		return update;
	}
	
	/**
	 * 用MchId查询未交费的订单
	 * 
	 * @param MchId 
	 * @return
	 * @throws InterruptedException
	 */
	public List<subscriptioncharge> selectSubscriptionChargeNotPay(Integer MchId) throws InterruptedException {
		subscriptionchargeExample pe = new subscriptionchargeExample();
		pe.setOrderByClause("`UpdateTime` DESC");
		com.ymttest.database.model.godmch.subscriptionchargeExample.Criteria c = pe.createCriteria();
		c.andMchIdEqualTo(MchId);
		c.andPaymentIdIsNull();
		c.andExpirationDayEqualTo(365);
		List<subscriptioncharge> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.subscriptionchargeMapper.selectByExample", pe);
		manage.commit();
		return list;
	}

	/**
	 * 根据roleid查询Permission
	 * 
	 * @param roleid
	 * @return
	 * @throws InterruptedException
	 */
	public List<permission> selectPermission(Integer roleid) throws InterruptedException {
		Map m = new HashMap();
		m.put("roleid", roleid);
		List<permission> list = null;
		if (roleid == null || roleid == 0) {
			list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectDefPermission", m);
		} else {
			list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectPermission", m);
		}
		manage.commit();
		return list;
	}

	/**
	 * 查询权限信息
	 * 
	 * @param PermissionCode
	 * @return
	 * @throws InterruptedException
	 */
	public List<Permissionitem> selectPermissionitemByPermissionCode(Integer PermissionCode) throws InterruptedException {
		Map m = new HashMap();
		m.put("PermissionCode", PermissionCode);
		List<Permissionitem> list = null;

		if (PermissionCode == null) {
			list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectPermissionitem", null);
		} else {
			list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectPermissionitemByPermissionCode", m);
		}
		manage.commit();
		return list;
	}

	/**
	 * 查询PermissionCategory
	 * 
	 * @param cid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Map> selectPermissionCategory(Integer cid) throws InterruptedException {
		Map m = new HashMap();
		m.put("cid", cid);
		List<Map> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectPermissionCategory", m);
		manage.commit();
		return list;
	}

	/**
	 * 用MchId查询权限角色
	 * 
	 * @param MchId 0 默认权限列表 目前尚无分商户权限
	 * @return
	 * @throws InterruptedException
	 */
	public List<csrole> selectCsroleByMchId(Integer MchId) throws InterruptedException {
		Map m = new HashMap();
		m.put("MchId", MchId);
		List<csrole> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectCsroleByMchId", m);
		manage.commit();
		return list;
	}
	/**
	 * 查询客服信息
	 * @param mchid
	 * @param csid
	 * @return
	 * @throws InterruptedException
	 */
	public List<cs_info> selectCsInfo(Integer mchid,Integer csid) throws InterruptedException {
		Map m = new HashMap();
		m.put("csid", csid);
		m.put("mchid", mchid);
		List<cs_info> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectCsInfo", m);
		manage.commit();
		return list;
	}
	
	public  List<cs_info> selectCsInfoByMchId(Integer mchid) throws InterruptedException {
		Map m = new HashMap();
		m.put("mchid", mchid);
		List<cs_info> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectCsInfoByMchId", m);
		manage.commit();
		return list;
	}
	
	

	public List<Map> selectCsInfoByCsId(Integer csid) throws InterruptedException {
		Map m = new HashMap();
		m.put("csid", csid);
		List<Map> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectCsInfoByCsId", m);
		manage.commit();
		return list;
	}
	/**
	 * 查询代理类型
	 * @param mchId
	 * @param agentType
	 * @return
	 * @throws InterruptedException
	 */
	public List<agent_type> selectAgentType(Integer mchId,Integer agentType) throws InterruptedException {
		Map m = new HashMap();
		m.put("agentType", agentType);
		m.put("mchId", mchId);
		List<agent_type> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectAgentType", m);
		manage.commit();
		return list;
	}
	/**
	 * @param mchId
	 * @param agentType
	 * @return
	 * @throws InterruptedException
	 */
	public List<invite_code> selectInviteCode(Integer mchId,Integer agentType) throws InterruptedException {
		Map m = new HashMap();
		m.put("agentType", agentType);
		m.put("mchId", mchId);
		List<invite_code> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectInviteCode", m);
		manage.commit();
		return list;
	}
	/**
	 * 查询代理关系
	 * @param parentMchId 渠道商id 主商户
	 * @param mchId 代理商id
	 * @param agentType 代理类型
	 * @return
	 * @throws InterruptedException
	 */
	public List<mch_relation> selectMchRelation(Integer parentMchId,Integer mchId,Integer agentType) throws InterruptedException {
		Map m = new HashMap();
		m.put("agentType", agentType);
		m.put("mchId", mchId);
		m.put("parentMchId", parentMchId);
		List<mch_relation> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectMchRelation", m);
		manage.commit();
		return list;
	}
	
	/**
	 * 查询商家信息
	 * @param mchId
	 * @return
	 * @throws InterruptedException
	 */
	public List<mch_info> selectMchInfo(Integer mchId) throws InterruptedException {
		Map m = new HashMap();
		m.put("mchId", mchId);
		List<mch_info> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectMchInfo", m);
		manage.commit();
		return list;
	}
	/**查询国家信息
	 * @param country 国家
	 * @param currency 币种
	 * @return
	 * @throws InterruptedException
	 */
	public List<country_info> selectCountryInfo(String country,String currency) throws InterruptedException {
		Map m = new HashMap();
		m.put("country", country);
		m.put("currency", currency);
		List<country_info> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.permissionMapper.selectCountryInfo", m);
		manage.commit();
		return list;
	}
	
	
	public int updateByPrimaryKey(mch_info record) throws InterruptedException {

		int result = manage.update("com.ymttest.database.mapping.godmch.permissionMapper.updateByPrimaryKey",record);
		manage.commit();
		return result;
	}
	/**
	 * 获取MchApp（商户小程序信息）
	 * @param mchid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Mchapp> selectMchappByExample(Integer mchid) throws InterruptedException {
		MchappExample mchappExample=new MchappExample();
		com.ymttest.database.model.MchappExample.Criteria c = mchappExample.createCriteria();
		if(mchid!=null) {
			c.andMchIdEqualTo(mchid);
		}
		mchappExample.setOrderByClause("UpdateTime desc limit 10");
		List<Mchapp>	list = manage.getSelectResults("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.selectMchappByExample", mchappExample);
		manage.commit();
		return list;
	}
	
	
	/**
	 * 修改mchapp，根据主键
	 * @param mchid
	 * @return
	 * @throws InterruptedException
	 */
	public int updateMchAppByPrimaryKey(Mchapp mchapp) throws InterruptedException {

		int result = manage.update("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.updateMchappByPrimaryKeySelective",mchapp);
		manage.commit();
		return result;
	}
	
	/**
	 * 获取MchApp已经发布的前N条（商户小程序信息）
	 * @param mchid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Mchapp> selectMchappByPublish(Integer count) throws InterruptedException {
		MchappExample mchappExample=new MchappExample();
		com.ymttest.database.model.MchappExample.Criteria c = mchappExample.createCriteria();
		c.andIsPublishedEqualTo(true);
		c.andAppStatusEqualTo(5);
		mchappExample.setOrderByClause("UpdateTime desc limit "+count);
		List<Mchapp>	list = manage.getSelectResults("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.selectMchappByExample", mchappExample);
		manage.commit();
		return list;
	}
	
	
	/**
	 * 获取MchApp可以重新发布的前N条（商户小程序信息）
	 * @param mchid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Mchapp> selectMchappBycount(Integer count) throws InterruptedException {
		MchappExample mchappExample=new MchappExample();
		com.ymttest.database.model.MchappExample.Criteria c = mchappExample.createCriteria();
		c.andIsPublishedEqualTo(true);
		mchappExample.setOrderByClause("UpdateTime desc limit "+count);
		List<Mchapp>	list = manage.getSelectResults("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.selectMchappByExample", mchappExample);
		manage.commit();
		return list;
	}
	
	//deleteMchappByMchid
	public int deleteMchappByMchid(Integer mchid) throws InterruptedException {
		int i= manage.delete("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.deleteMchappByMchid", mchid);
		manage.commit();
		return i;
	}
	/**
	 * 获取PlatformConfig（平台配置）
	 * @return
	 * @throws InterruptedException
	 */
	public List<Platformconfig> selectPlatformconfigByExample() throws InterruptedException {
		PlatformconfigExample PlatformconfigExample=new PlatformconfigExample();
		com.ymttest.database.model.PlatformconfigExample.Criteria c = PlatformconfigExample.createCriteria();
		PlatformconfigExample.setOrderByClause("UpdateTime desc limit 10");
		List<Platformconfig>	list = manage.getSelectResults("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.selectPlatformconfigByExample", PlatformconfigExample);
		manage.commit();
		return list;
	}
	//selectMchChargeRecordIdByExample
	/**
	 * 查询最近10条充值记录
	 * @param mchid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Mchchargerecord> selectMchChargeRecordIdByExample(Integer mchid) throws InterruptedException {
		MchchargerecordExample MchchargerecordExample=new MchchargerecordExample();
		com.ymttest.database.model.MchchargerecordExample.Criteria c = MchchargerecordExample.createCriteria();
		if(mchid!=null) {
			c.andMchIdEqualTo(mchid);
		}
		MchchargerecordExample.setOrderByClause("UpdateTime desc limit 10");
		List<Mchchargerecord>	list = manage.getSelectResults("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.selectMchChargeRecordIdByExample", MchchargerecordExample);
		manage.commit();
		return list;
	}
	
	//selectAuditrecordByExample
	/**获取AuditRecord（小程序提审记录）
	 * @param mchid
	 * @return
	 * @throws InterruptedException
	 */
	public List<Auditrecord> selectAuditrecordByExample(Integer mchid) throws InterruptedException {
		AuditrecordExample AuditrecordExample=new AuditrecordExample();
		com.ymttest.database.model.AuditrecordExample.Criteria c = AuditrecordExample.createCriteria();
		if(mchid!=null) {
			c.andMchIdEqualTo(mchid);
		}
		AuditrecordExample.setOrderByClause("UpdateTime desc limit 10");
		List<Auditrecord>	list = manage.getSelectResults("com.ymttest.database.mapping.wxcomponent.WxMchAutoMapper.selectAuditrecordByExample", AuditrecordExample);
		manage.commit();
		return list;
	}
	
	
	public List<chargeconfig> getChargeConfig(chargeconfigExample example) throws InterruptedException {
		Map m = new HashMap();
		List<chargeconfig> list = manage.getSelectResults("com.ymttest.database.mapping.godmch.chargeconfigMapper.selectByExample", example);
		manage.commit();
		return list;
	}
	
	public static void main(String args[]) throws InterruptedException {
		System.out.println(FormatUtil.beanToGSONString(new DefGodMchWapper().selectMchappByExample(null)));
		System.out.println(FormatUtil.beanToGSONString(new DefGodMchWapper().selectPlatformconfigByExample()));
		System.out.println(FormatUtil.beanToGSONString(new DefGodMchWapper().selectAuditrecordByExample(null)));
		System.out.println(FormatUtil.beanToGSONString(new DefGodMchWapper().selectMchChargeRecordIdByExample(500009815)));
		
	}
}
